package com.demo.blog;

import com.demo.common.model.Person;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.sql.SQLException;

/**
 * @author liliang
 * @description:
 * @date 2018/6/1.
 */
public class PersonService {

    public static final PersonService personService = new PersonService();
    private BlogService blogService = BlogService.me;

    private Person dao = new Person().dao();

    public boolean save(Person person) {
        Record user = new Record().set("id", person.getId()).set("name", person.getName()).set("age", person.getAge());
        Db.save("person", user);
        Record blog = new Record().set("id", null).set("title", "大标题").set("content", "这是标题内容.....");
        return Db.save("blog",blog);
        //        int i = 1 / 0;
//        return person.save();
    }

    /**
     * 分页查询
     *
     * @param pageNumber 页码
     * @param pageSize   页大小
     * @return 分页参数
     */
    public Page<Person> paginate(int pageNumber, int pageSize) {
        return dao.paginate(pageNumber, pageSize, "SELECT *", "from person order by id asc");
    }

    /**
     * 根据 ID 查询
     *
     * @param id
     * @return
     */
    public Person findById(Integer id) {
        return dao.findById(id);
    }

    /**
     * 根据 ID 删除
     *
     * @param id
     */
    public boolean deleteById(Integer id) {
        return dao.deleteById(id);
    }

    /**
     * 更新
     *
     * @param person
     * @return
     */
    public boolean update(Person person) {
        return person.update();
    }
}
